package com.mushuroom.live.controller;

import com.mushroom_street.live.dto.LiveDto;
import com.mushroom_street.live.pojo.Live;
import com.mushroom_street.live.pojo.LiveProduct;
import com.mushroom_street.product.dto.ProductDto;
import com.mushuroom.live.feign.ProductService;
import com.mushuroom.live.service.LiveProductService;
import com.mushuroom.live.service.LiveService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping(value = "/live")
public class LiveController {
    @Resource
    private LiveService liveService;
    @Resource
    private LiveProductService liveProductService;
    @Resource
    private ProductService productService;
    @GetMapping("/lives")
   public List<LiveDto> loadLiveList(){
        //创建集合保存返回给前端的直播信息
        ArrayList<LiveDto> liveDtoList=new ArrayList<>();
        LiveDto liveDto = null;
        //查询到所有的直播信息
        List<Live> lives=liveService.loadLiveList();
        //根据直播信息查询主播带货商品id
        //遍历主播集合，取出主播id
        for (Live live : lives){
            if (live != null){
                //获取主播带货商品集合
                List<LiveProduct> liveProducts=liveProductService.loadLiveProductByLiveId(live.getLiveId());
                liveDto = new LiveDto();
                liveDto.setLive(live);
                //创建集合保存主播带货的商品
                ArrayList<ProductDto> dtoArrayList = new ArrayList<>();
                //取出带货商品集合中的所有商品id
                for (LiveProduct liveProduct:liveProducts){
                    if (liveProduct != null){
                        //调用商品服务查商品信息
                        ProductDto productDto = productService.loadProductById(liveProduct.getProductId());
                        dtoArrayList.add(productDto);
                    }
                }
                liveDto.setProductDtoList(dtoArrayList);
                liveDtoList.add(liveDto);
            }

        }
        //根据商品服务查询商品信息
        return  liveDtoList;
    }

}
